#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include"Lock.hpp"
//四个线程同时访问统一资源造成bug
static int tickets=1000;

void *routie(void *arg)
{
    Mutex metex;
    char *name = (char *)arg;
    while (1)
    {
        metex.lock();
        if (tickets > 0)
        {
            usleep(1000);
            
            tickets--;
            std::cout<<name<<"sells tickets:"<<tickets<<std::endl;
            metex.unlock();
        }
        else
        {
            metex.unlock();
            break;
        }
    }
    return nullptr;
}
int main()
{
    pthread_t tid1, tid2, tid3, tid4, tid5;

    pthread_create(&tid1, NULL, routie, (void *)"thread-1"); 
    pthread_create(&tid1, NULL, routie, (void *)"thread-2"); 
    pthread_create(&tid1, NULL, routie, (void *)"thread-3"); 
    pthread_create(&tid1, NULL, routie, (void *)"thread-4"); 

    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    pthread_join(tid3,NULL);
    pthread_join(tid4,NULL);
    return 0;
}